package com.twt.sys.base;

import com.twt.common.pojo.Result;
import com.twt.model.base.BaseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public abstract class BaseController<T extends BaseEntity> {
    // 这些对象何以直接被子类使用
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;

    @ModelAttribute
    public void setReqAndRes(HttpServletRequest req, HttpServletResponse res) {
        this.request = req;
        this.response = res;
        this.session = req.getSession();
    }

    protected abstract BaseService getBaseService();


    /**
     * @Description: 添加
     * @Author: TWT
     * @Date: 22:30 2022/6/16
     * @param:[sysParamsPo]
     * @return:com.twt.common.pojo.Result
     **/
    @PostMapping("/add")
    public Result add(@Validated T po) {
        this.getBaseService().add(po);
        return Result.ok();
    }

    /**
     * @Description: 修改
     * @Author: TWT
     * @Date: 22:31 2022/6/16
     * @param:[sysParamsPo]
     * @return:com.twt.common.pojo.Result
     **/
    @PutMapping("/update")
    public Result update(T po) {
        this.getBaseService().editById(po);
        return Result.ok();
    }

    /**
     * @Description: 批量删除
     * @Author: TWT
     * @Date: 23:12 2022/6/16
     * @param:[ids]
     * @return:com.twt.common.pojo.Result
     **/
    @DeleteMapping("/deleteByIds")
    public Result deleteByIds(String ids) {
        this.getBaseService().deleteByIds(ids);
        return Result.ok();
    }

    /**
     * @Description: 查询单个数据
     * @Author: TWT
     * @Date: 23:23 2022/6/16
     * @param:[id]
     * @return:com.twt.common.pojo.Result
     **/
    @GetMapping("/query/one/{id}")
    public Result queryOneById(@PathVariable("id") String id) {
        Object obj = this.getBaseService().queryOneById(id);
        return Result.ok(obj);
    }

}
